<?php
//输入过滤快捷方法
function i($key,$default_val='',$request='get'){
	if($request == 'get'){
		$_GET[$key] = empty($_GET[$key]) ? $default_val : $_GET[$key];
		return $_GET[$key];
	}
	if($request == 'post'){
		$_POST[$key] = empty($_POST[$key]) ? $default_val : $_POST[$key];
		return $_POST[$key];
	}
    if($request == 'request'){
        $_REQUEST[$key] = empty($_REQUEST[$key]) ? $default_val : $_REQUEST[$key];
        return $_REQUEST[$key];
    }
	if($request == 'cookie'){
		$_COOKIE[$key] = empty($_COOKIE[$key]) ? $default_val : $_COOKIE[$key];
		return $_COOKIE[$key];
	}
	if($request == 'session'){
		$_SESSION[$key] = empty($_SESSION[$key]) ? $default_val : $_SESSION[$key];
		return $_SESSION[$key];
	}
}

//错误提示
$err_msg = array(
    '000'=>'操作成功',
    '001'=>'无权限访问',

    '100'=>'请先登录',
    '101'=>'请输入用户名',
    '102'=>'请输入密码',
    '103'=>'用户名或密码不正确',
    '104'=>'手机号已被占用',
    '105'=>'手机号格式不正确',
    '106'=>'请选择优惠券',
    '107'=>'请输入邮箱地址',
    '108'=>'用户不存在',
    '109'=>'新密码与确认密码不一致',
    '111'=>'旧密码不正确',

    '110'=>'请输入联系人',

    '200'=>'该商品不存在',
    '201'=>'请选择订餐时间',
    '202'=>'请输入用餐人数',

    '300'=>'请填写收货人名字',
    '301'=>'请选择省份',
    '302'=>'请选择城市',
    '303'=>'请选择地区',
    '304'=>'请填写地址',
    '305'=>'手机号不能为空',
    '306'=>'余额不足，请先充值',
    
    '307'=>'订单金额未达到优惠券使用额度',
    '308'=>'优惠券不存在或已被使用',

    '350'=>'购物车是空的，请先选购商品',

);
function to_ajax($state='1',$err_code='000',$data){
    $msg = isset($GLOBALS['err_msg'][$err_code]) ? $GLOBALS['err_msg'][$err_code] : $GLOBALS['err_msg']['000'];

	header('Content-Type:application/json; charset=utf-8');
	$result = array('state'=>(string)$state, 'err_code'=>(string)$err_code, 'msg'=>(string)$msg, 'data'=>(object)$data);
    //jsonp
    //$callback = $_GET['callback'];
    //exit($callback.'('.json_encode($result).')');

    //json
	exit(json_encode($result));
}

//获取客户端IP
function get_client_ip($type = 0) {
    $type       =  $type ? 1 : 0;
    static $ip  =   NULL;
    if ($ip !== NULL) return $ip[$type];
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $arr    =   explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos    =   array_search('unknown',$arr);
        if(false !== $pos) unset($arr[$pos]);
        $ip     =   trim($arr[0]);
    }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ip     =   $_SERVER['HTTP_CLIENT_IP'];
    }elseif (isset($_SERVER['REMOTE_ADDR'])) {
        $ip     =   $_SERVER['REMOTE_ADDR'];
    }
    // IP地址合法验证
    $long = sprintf("%u",ip2long($ip));
    $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
    return $ip[$type];
}

//生成唯一订单号
function order_no()
{
    /* 选择一个随机的方案 */
    mt_srand((double) microtime() * 1000000);
    //return date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    return date('Ymd') . uniqid();
}

//生成优惠券码
function coupon_no() {
    $code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $rand = $code[rand(0,25)]
        .strtoupper(dechex(date('m')))
        .date('d').substr(time(),-5)
        .substr(microtime(),2,5)
        .sprintf('%02d',rand(0,99));
    for(
        $a = md5( $rand, true ),
        $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
        $d = '',
        $f = 0;
        $f < 8;
        $g = ord( $a[ $f ] ),
        $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
        $f++
    );
    return $d;
}

//获取会员等级价格
function level_discount($user_level){
    global $dou;
    $defined = $dou->get_one("SELECT value FROM " . $dou->table('config') . " WHERE name='defined'");
    $defined = unserialize($defined);
    $defined_user_level_discount = explode(',', $defined['user_level_discount']);

    $discount = str_replace("$user_level|","", $defined_user_level_discount[$user_level]);
    $discount = !$discount ? "100" : $discount;
    return $discount/100;
}

//获取地区信息
function get_region_info($province, $city, $area){
    $sql = "SELECT * FROM " . $GLOBALS['dou']->table('region') . " WHERE region_id IN('$province','$city','$area')";
    $list = $GLOBALS['dou']->get_all($sql);
    $arr = array('province'=>'', 'city'=>'', 'area'=>'');
    if($list){
        foreach($list as $k=>$v){
            if($v['region_type'] == 1) $arr['province'] = $v['region_name'];
            if($v['region_type'] == 2) $arr['city'] = $v['region_name'];
            if($v['region_type'] == 3) $arr['area'] = $v['region_name'];
        }
    }
    return $arr;
}

//取出二维数组一列
if(!function_exists('array_column'))
{
  function array_column($input, $columnKey, $indexKey = NULL)
  {
    $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE;
    $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE;
    $indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE;
    $result = array();
 
    foreach ((array)$input AS $key => $row)
    { 
      if ($columnKeyIsNumber)
      {
        $tmp = array_slice($row, $columnKey, 1);
        $tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL;
      }
      else
      {
        $tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL;
      }
      if ( ! $indexKeyIsNull)
      {
        if ($indexKeyIsNumber)
        {
          $key = array_slice($row, $indexKey, 1);
          $key = (is_array($key) && ! empty($key)) ? current($key) : NULL;
          $key = is_null($key) ? 0 : $key;
        }
        else
        {
          $key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
        }
      }
 
      $result[$key] = $tmp;
    }
 
    return $result;
  }
}
//删除空格
function trimall($str)
{
    $qian=array(" ","　","\t","\n","\r");$hou=array("","","","","");
    return str_replace($qian,$hou,$str);    
}